﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace SurehealthForms
{
    public partial class PharmacistAddPrescip : Form
    {
        #region variables
        List<string> PrescriptionList = new List<string>();
        List<CashierLogin.perscriptionItem> _ItemList = new List<CashierLogin.perscriptionItem>();
        int ItemNo = 1;
        int ItemCount = 0;
        int numbOfItems = 1;
        bool perscriptionStockEnough;
        #endregion

        /// <summary>
        /// contstructor
        /// </summary>
        public PharmacistAddPrescip()
        {
            InitializeComponent();
        }

        /// <summary>
        /// when form loads load the list of available prescriptions
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PharmacistAddPrescip_Load(object sender, EventArgs e)
        {
            RefreshList();
        }

        /// <summary>
        /// refreshes the list of available prescriptions
        /// </summary>
        void RefreshList()
        {
            PrescriptionList.Clear();
            StreamReader Reader = new StreamReader("AvailablePrescriptions.txt");
            while (Reader.EndOfStream == false)
            {
                PrescriptionList.Add(Reader.ReadLine());
            }

            foreach (string s in PrescriptionList)
            {
                AvailableDrop.Items.Add(s);
            }
            Reader.Close();
            Reader.Dispose();
        }

        /// <summary>
        /// finalises a prescription to go to a form to be checked whether
        /// it has been picked up if it hasn't it can be returned to the stock
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Finalise_Click(object sender, EventArgs e)
        {
            if (PrescriptionList.Count == 0)
            {
                MessageBox.Show("No Prescriptions to be made up");
            }
            else
            {
                if (perscriptionStockEnough == true)
                {
                    if (DodgyBobStockControl.Stock.CheckInDate(textBox3.Text) == true)
                    {
                        #region remove stock
                        bool stockFailedtoRemove = false;
                        foreach (CashierLogin.perscriptionItem I in _ItemList)
                        {
                            if (!DodgyBobStockControl.Stock.UseStock(I.Item, int.Parse(I.Quantity)))
                                stockFailedtoRemove = true;
                        }

                        if (stockFailedtoRemove)
                            MessageBox.Show("This stock has not been removed");
                        else
                            MessageBox.Show("Perscription has been finalized");


                        #endregion

                        PrescriptionList.Remove(AvailableDrop.Text);
                        List<string> ReadyForCollection = new List<string>();
                        StreamReader PerscriptionsForcollectionReader = new StreamReader("PerscriptionsForCollection.txt");
                        while (PerscriptionsForcollectionReader.EndOfStream == false)
                        {
                            ReadyForCollection.Add(PerscriptionsForcollectionReader.ReadLine());
                        }
                        PerscriptionsForcollectionReader.Close();


                        StreamWriter PerscriptionsForcollection = new StreamWriter("PerscriptionsForCollection.txt");
                        ReadyForCollection.Add(AvailableDrop.Text);

                        foreach (string S in ReadyForCollection)
                        {
                            PerscriptionsForcollection.WriteLine(S);
                        }
                        PerscriptionsForcollection.Close();

                        StreamWriter Writer = new StreamWriter("AvailablePrescriptions.txt");
                        foreach (string S in PrescriptionList)
                        {
                            Writer.WriteLine(S);
                        }
                        Writer.Close();
                        Writer.Dispose();


                        RefreshList();
                        AvailableDrop.Text = "";
                        textBox1.Text = "";
                        textBox4.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                        textBox5.Text = "";
                        label1.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("There is not enough stock in place for this sorry");
                    }
                }
                else
                {
                    MessageBox.Show("You are trying to finalise a Perscription that we don't have stock for please order some more");
                }
            }
        }

        /// <summary>
        /// Loads the prescription in to the the boxes for the pharmacist
        /// to make up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            perscriptionStockEnough = true;//pressume that there is enough stock if there isn't then
            //it will be changed and the prescription will go no further.

            #region clear the list
            _ItemList.Clear();
            //foreach (CashierLogin.perscriptionItem I in _ItemList)
            //{
            //    if(_ItemList.Count>0)
            //    _ItemList.Remove(I);
            //}
            #endregion

            if (AvailableDrop.Text != "")
            {
                #region initial xml 
                XmlDocument document = null; ;
                document = new XmlDocument();
                document.Load(AvailableDrop.Text + ".Xml");
                System.Xml.XmlElement root = document.DocumentElement;
                #endregion

                #region load data about prescription to strings
                string patientname = root.GetAttribute("Name");
                string Doctorname = root.GetAttribute("Doctor");
                string DateSet = root.GetAttribute("Date");
                ItemCount = int.Parse(root.GetAttribute("NumberofItems"));
                string Discount = root.GetAttribute("FullDiscount");
                #endregion

                XmlNodeList nodes = document.GetElementsByTagName("PrescriptionITEM");//gt all prescriptions items

                int itemNo = 1;//set it back to one so that we are at the start of the list

                #region load each prescription item
                foreach (XmlNode n in nodes)
                {

                    XmlElement Item = document.DocumentElement;

                    string Quantity = n.Attributes["Quantity"].InnerText;
                    string StockItem = n.Attributes["SItem"].InnerText;
                    if (DodgyBobStockControl.Stock.StockCheck(StockItem) < int.Parse(Quantity))
                    {
                        perscriptionStockEnough = false;//if stock is less then required this is set to false.
                        MessageBox.Show("There is not enough stock of " + StockItem + " to fulfill this perscription,please order more");
                    }
                    string Notes = n.Attributes["Notes"].InnerText;
                    _ItemList.Add(new CashierLogin.perscriptionItem(itemNo.ToString(), 
                                Quantity, StockItem, Notes));

                }

                #endregion

                #region load data from the prescriptions in to the text boxes
                textBox1.Text = patientname;
                textBox4.Text = DateSet;
                
                itemNo = 1;
                if (itemNo == 1)
                {
                    label1.Text = "1/" + ItemCount.ToString();
                    textBox2.Text = _ItemList[0].Quantity;
                    textBox3.Text = _ItemList[0].Item;
                    textBox5.Text = _ItemList[0].Description;
                }

                #endregion
            }
            else
            {
                MessageBox.Show("Please select a prescription");
            }
        }

        /// <summary>
        /// moves to the next item the prescription for the pharmacist to make up
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextItem_Click_1(object sender, EventArgs e)
        {
            ItemNo++;

            if (ItemNo <= ItemCount)
            {
                textBox2.Text = _ItemList[ItemNo - 1].Quantity;
                textBox3.Text = _ItemList[ItemNo - 1].Item;
                textBox5.Text = _ItemList[ItemNo - 1].Description;
                label1.Text = ItemNo + "/" + ItemCount;
            }
            else
                ItemNo--;

        }

        /// <summary>
        /// moves back for the pharmacist to make sure they didn't make a mistake
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PreviousItem_Click(object sender, EventArgs e)
        {  
            ItemNo--;

            if (ItemNo >= 1)
            {
                textBox2.Text = _ItemList[ItemNo - 1].Quantity;
                textBox3.Text = _ItemList[ItemNo - 1].Item;
                textBox5.Text = _ItemList[ItemNo - 1].Description;
                label1.Text = ItemNo + "/" + ItemCount;
            }
            else
                ItemNo++;
        }

    }
}
